package ynu.edu.controller;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import ynu.edu.dto.FoodCategoryDTO;
import ynu.edu.service.FoodCategoryService;

import jakarta.validation.Valid;
import java.util.List;

/**
 * 食品分类控制器
 */
@RestController
@RequestMapping("/foodCategory")
@RequiredArgsConstructor
@Slf4j
public class FoodCategoryController {
    
    private final FoodCategoryService foodCategoryService;
    
    /**
     * 根据商家ID获取食品分类列表
     *
     * @param businessId 商家ID
     * @return 食品分类列表
     */
    @GetMapping("/getByBusinessId/{businessId}")
    public ResponseEntity<List<FoodCategoryDTO>> getCategoriesByBusinessId(@PathVariable Integer businessId) {
        log.info("根据商家ID获取食品分类列表，businessId: {}", businessId);
        List<FoodCategoryDTO> categories = foodCategoryService.getCategoriesByBusinessId(businessId);
        log.info("获取到{}个食品分类", categories.size());
        return ResponseEntity.ok(categories);
    }
    
    /**
     * 根据ID获取食品分类
     *
     * @param categoryId 分类ID
     * @return 食品分类信息
     */
    @GetMapping("/getById/{categoryId}")
    public ResponseEntity<FoodCategoryDTO> getCategoryById(@PathVariable Integer categoryId) {
        log.info("根据ID获取食品分类，categoryId: {}", categoryId);
        FoodCategoryDTO category = foodCategoryService.getCategoryById(categoryId);
        log.info("获取到食品分类: {}", category.getName());
        return ResponseEntity.ok(category);
    }
    
    /**
     * 添加食品分类
     *
     * @param categoryDTO 食品分类信息
     * @return 添加后的食品分类信息
     */
    @PostMapping("/add")
    public ResponseEntity<FoodCategoryDTO> addCategory(@Valid @RequestBody FoodCategoryDTO categoryDTO) {
        log.info("添加食品分类: {}, 所属商家ID: {}", categoryDTO.getName(), categoryDTO.getBusinessId());
        FoodCategoryDTO addedCategory = foodCategoryService.addCategory(categoryDTO);
        log.info("食品分类添加成功，ID: {}", addedCategory.getId());
        return new ResponseEntity<>(addedCategory, HttpStatus.CREATED);
    }
    
    /**
     * 更新食品分类
     *
     * @param categoryDTO 食品分类信息
     * @return 更新后的食品分类信息
     */
    @PutMapping("/update")
    public ResponseEntity<FoodCategoryDTO> updateCategory(@Valid @RequestBody FoodCategoryDTO categoryDTO) {
        log.info("更新食品分类，categoryId: {}, 分类名: {}", categoryDTO.getId(), categoryDTO.getName());
        FoodCategoryDTO updatedCategory = foodCategoryService.updateCategory(categoryDTO);
        log.info("食品分类更新成功");
        return ResponseEntity.ok(updatedCategory);
    }
    
    /**
     * 删除食品分类
     *
     * @param categoryId 分类ID
     * @return 操作结果
     */
    @DeleteMapping("/delete/{categoryId}")
    public ResponseEntity<Void> deleteCategory(@PathVariable Integer categoryId) {
        log.info("删除食品分类，categoryId: {}", categoryId);
        foodCategoryService.deleteCategory(categoryId);
        log.info("食品分类删除成功");
        return ResponseEntity.noContent().build();
    }
} 